<?php

class __photos_ajax extends Module {
	
	public function __construct () {
		parent::__construct ('photos');
	}
	
	public function ajax_addcomment () {
		$typesController = TypesController::getInstance ();
		$objectsController = ObjectsController::getInstance ();
		
		$oType = $typesController->getTypeByName ('photo');
		
		$requestObjectId = getRequest ('param0', 'int');
		
		$oCommentType = $typesController->getTypeByName ('comment');
		
		$testSel = new Selection;
		$testSel->addType ($oType);
		$testSel->addFilter ('id', (int) $requestObjectId);
				
		if ($testSel->getTotalCount () == 0) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml'); 
		}
		
		$oComment = new Object ($oCommentType);
		$oComment->setValues ($this->getRequestData ('new', $oCommentType, true));
		$oComment->setValue ('owner_id', ExplaySession::getUserId ());
		$oComment->setValue ('pubdate', time ());
		$oComment->setValue ('type_id', $oType->getId ());
		$oComment->setValue ('object_id', $requestObjectId);
		
		if (count ($this->getValidateErrors ($oComment, $oCommentType)) > 0) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml'); 
		}
		
		if ($oComment->getValue ('rel_id') != 0 && !$oRelComment = $objectsController->getObject ($oCommentType, $oComment->getValue ('rel_id'))) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml'); 
		}
		
		$oComment->createNew ();
		
		$oPhoto = $objectsController->getObject ($oType, $requestObjectId);
		$countComments = $oPhoto->getValue ('count_comments');
		$oPhoto->setValue ('count_comments', ++$countComments);
		$oPhoto->save ();
		
		if ($oComment->getValue ('rel_id') == 0) {
			$this->photoReplyNotification ($oPhoto, $oComment);
		} else {
			$this->commentReplyNotification ($oPhoto, $oRelComment, $oComment);
		}
		
		$this->flush ((string) new Array2XML ('data', array ('result' => '1')), 'text/xml');		
	}
	
	public function ajax_set_album_preview () {
		$this->expectSecureGET();
		
		$typesController = TypesController::getInstance ();
		$oTypePhoto = $typesController->getTypeByName ('photo');
		$oTypeAlbum = $typesController->getTypeByName ('photoalbum');
		$objectsController = ObjectsController::getInstance ();
		
		$albumId = getRequest ('param0', 'int');
		$photoId = getRequest ('param1', 'int');
		
		if (!$oAlbum = $objectsController->getObject ($oTypeAlbum, $albumId)) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml'); 
		}
		
		if ($oAlbum->getValue ('owner_id') != ExplaySession::getUserId ()) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml'); 
		}
		
		if (!$oPhoto = $objectsController->getObject ($oTypePhoto, $photoId)) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml'); 
		}
		
		if ($oPhoto->getValue('album_id') != $albumId) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml');
		}
		
		$oAlbum->setValue('preview', $oPhoto->getValue ('photo'));
		$oAlbum->save ();
		
		$this->flush ((string) new Array2XML ('data', array ('result' => '1')), 'text/xml');
	}
	
	public function ajax_delete_photo() {
		$this->expectSecureGET();
		
		$typesController = TypesController::getInstance();
		$oTypePhoto = $typesController->getTypeByName('photo');
		$oTypeAlbum = $typesController->getTypeByName('photoalbum');
		$objectsController = ObjectsController::getInstance();
		
		$photoId = getRequest('param0');
		
		if (!$oPhoto = $objectsController->getObject($oTypePhoto, $photoId)) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml');
		}
		
		$userGroup = userGroup (ExplaySession::getUser()->getValue ('group'));
		
		if ($oPhoto->getValue ('owner_id') != ExplaySession::getUserId() && $userGroup != 'admin' && $userGroup != 'moderator') {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml');
		}
		
		$albumId = $oPhoto->getValue('album_id');
		
		$oPhoto->delete();
		
		$oAlbum = $objectsController->getObject($oTypeAlbum, $albumId);
		$cntPhotos = $oAlbum->getValue('count_photos') - 1;
		$oAlbum->setValue('count_photos', $cntPhotos);
		$oAlbum->save();
		
		$this->flush ((string) new Array2XML ('data', array ('result' => '1')), 'text/xml');
	}
	
	/**
	 * Голосование за фотографию
	 * @return void
	 */
	public function ajax_vote_photo () {
		$this->expectSecureGET();
		
		$typesController = TypesController::getInstance ();
		$objectsController = ObjectsController::getInstance ();

		$photoId = getRequest ('param0', 'int');
		$mark = getRequest ('param1', 'int');
		
		$oTypePhoto = $typesController->getTypeByName ('photo');
		
		if (!$oPhoto = $objectsController->getObject ($oTypePhoto, $photoId)) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml');
		}
		
		$oVoteType = $typesController->getTypeByName ('vote');
		
		$sel = new Selection;
		$sel->addType ($oVoteType);
		$sel->addFilter ('user_id', ExplaySession::getUserId ());
		$sel->addFilter ('object_id', $photoId);
		$sel->addFilter ('type_id', $oTypePhoto->getId ());
		
		if ($sel->getTotalCount () > 0) {
			$this->flush ((string) new Array2XML ('data', array ('result' => '0')), 'text/xml');
		}
		
		$oVote = new Object ($oVoteType);
		$oVote->setValues (array (
			'type_id' => $oTypePhoto->getId (),
			'object_id' => $photoId,
			'user_id' => ExplaySession::getUserId (),
			'mark' => $mark == 1 ? '1' : '0'
		));
		$oVote->createNew ();
		
		$rating = $oPhoto->getValue ('rating');
		$rating = $mark == 1 ? ++$rating : --$rating;
		$oPhoto->setValue ('rating', $rating);
		$oPhoto->save ();
		
		$format = number_format ($rating, 0, ',', ' ');
		$format = str_replace('-', '−', $format);
		
		$this->flush ((string) new Array2XML ('data', array ('result' => '1', 'rating' => $rating, 'format' => $format)), 'text/xml');
	}
	
	public function ajax_get_album_photos() {
		$oTypePhoto = TypesController::getInstance()->getTypeByName('photo');
		
		$sel = new Selection;
		$sel->addType($oTypePhoto);
		$sel->addFilter('album_id', getRequest('param0','int'));
		
		$aObjects = ObjectsController::getInstance()->getObjectsList($sel->getList());
		
		$response = new ModuleResponse;
		$response->setTemplate('photos/browserPhotosList');
		
		foreach ($aObjects as $object) {
			$response->addObject($object);
		}
		
		return $response;
	}
	
	public function ajax_photo_tickets() {
		$this->setResponseFormat('xml');
		
		$typesController = TypesController::getInstance();
		$oTypeTicket = $typesController->getTypeByName('photo_ticket');
		
		$photoId = getRequest('param0','int');
		
		$sel = new Selection;
		$sel->addType($oTypeTicket);
		$sel->addFilter('photo_id', $photoId);
		
		$aObjects = ObjectsController::getInstance()->getObjectsList($sel->getList());
		
		$response = new ModuleResponse;
		
		foreach ($aObjects as $object) {
			$response->addObject($object);
		}
		
		return $response;
	}
	
	public function ajax_get_ticket() {
		$this->setResponseFormat('xml');
		
		$typesController = TypesController::getInstance();
		$oTypeTicket = $typesController->getTypeByName('photo_ticket');
		
		$ticketId = getRequest('param0','int');
		
		$response = new ModuleResponse;
		
		if ($object = ObjectsController::getInstance()->getObject($oTypeTicket, $ticketId)) {
			$response->addObject($object);
		}
		
		return $response;
	}
	
	public function ajax_add_ticket() {
		$typesController = TypesController::getInstance();
		$oTypeTicket = $typesController->getTypeByName('photo_ticket');
		$oTypeFriend = $typesController->getTypeByName('friend');
		
		$object = new Object($oTypeTicket);
		$object->setValues($this->getRequestData('new', $oTypeTicket));
		
		$userId = ExplaySession::getUserId();
		
		if ($userId != $object->getValue('user_id')) {
			$selFriend = new Selection;
			$selFriend->addType($oTypeFriend);
			$selFriend->addFilter('user_id', ExplaySession::getUserId ());
			$selFriend->addFilter('friend_id', $object->getValue('user_id'));
			$selFriend->addLimit(1);
			
			if (!$selFriend->getTotalCount()) {
				$this->flush((string) new Array2XML('data', array('result' => 0)), 'text/xml');
			}
		}
		
		$object->createNew();
		
		$this->flush((string) new Array2XML('data', array('result' => 1, 'id' => $object->getId())), 'text/xml');
	}
	
	public function ajax_delete_ticket() {
		$this->expectSecureGET();
		
		$typesController = TypesController::getInstance();
		$oTypeTicket = $typesController->getTypeByName('photo_ticket');
		
		$objectsController = ObjectsController::getInstance();
		
		$id = getRequest('param0','int');
		
		if (!$oTicket = $objectsController->getObject($oTypeTicket, $id)) {
			 $this->flush((string) new Array2XML('data', array('result' => 0)), 'text/xml');
		}
		
		if ($oTicket->getValue('user_id') != ExplaySession::getUserId()) {
			$oTypePhoto = $typesController->getTypeByName('photo');
			if (!$oPhoto = $objectsController->getObject($oTypePhoto, $oTicket->getValue('photo_id'))) {
				$this->flush((string) new Array2XML('data', array('result' => 0)), 'text/xml');
			}
			
			if ($oPhoto->getValue('owner_id') != ExplaySession::getUserId()) {
				$this->flush((string) new Array2XML('data', array('result' => 0)), 'text/xml');
			}
		}
		
		$oTicket->delete();
		
		$this->flush((string) new Array2XML('data', array('result' => 1)), 'text/xml');
	}
	
}